<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* @framework : Codeigniter Framework
*
* @author: fatangel26
*
*/
	class News extends Admin_Controller
	{
        public $upload_path;

		public function __construct()
		{
			parent::__construct();

            if(!$this->my_session->is_Admin())
            {
                redirect(base_url()."backend/auth/login");

                exit();
            }

            $this->load->model('backend/news_model', 'MNews', TRUE);

            $this->load->helper('text');

            $this->upload_path = 'assets/uploads/news/';
		}

        public function index()
        {
            $this->_data['title'] = 'Jewel | Danh sách tin tức';

            $this->load->model("backend/pages_model", "MPages", TRUE);

            $data['news'] = $this->MNews->select();

            $data['pages'] = $this->MPages->select();

            if(!$data['news'])
            {
                $data['msg'] = 'Không có dữ liệu để hiển thị.';
            }

            $this->_data['content'] = $this->load->view("backend/news/listnews", $data, TRUE);

            $this->load->view("backend/layout", $this->_data);
        }//End function index

        public function createnews()
        {
            $this->_data['title'] = 'Jewel | Thêm mới tin tức';

            $this->load->model("backend/pages_model", "MPages", TRUE);

            $data['pages'] = $this->MPages->select();

            /*Validate data*/
            $this->form_validation->set_rules('news_title','Tiêu đề', 'trim|required|min_length[5]|xss_clean');
            $this->form_validation->set_rules('news_title_slug','Tiêu đề không dấu', 'trim|required|min_length[5]|xss_clean');
            $this->form_validation->set_rules('news_image', 'Ảnh đại diện', 'trim|required|xss_clean');
            $this->form_validation->set_rules('news_image_thumb', 'Thumbnail', 'trim|required|xss_clean');
            $this->form_validation->set_rules('news_content', 'Nội dung', 'trim|required|min_length[5]');
            $this->form_validation->set_message('required','<span style="font-weight: bold">%s</span> không được để trống');
            $this->form_validation->set_message('min_length','<span style="font-weight: bold">%s</span> không được ít hơn 5 ký tự');

            if($this->form_validation->run() == FALSE)
            {
                $data['msg'] = '';
            }
            else
            {
                foreach($data['pages'] as $page):
                    if($page->pages_id == $this->input->post('news_pages', TRUE)):
                        $page_slug = $page->slug;
                        break;
                    endif;
                endforeach;

                $strInsert = array(
                    'news_title'        => $this->input->post('news_title', TRUE),
                    'news_title_slug'   => $this->input->post('news_title_slug', TRUE),
                    'news_image'        => $this->input->post('news_image', TRUE),
                    'news_image_thumb'  => $this->input->post('news_image_thumb', TRUE),
                    'news_content'      => $this->input->post('news_content'),
                    'news_status'       => $this->input->post('news_status', TRUE),
                    'news_hightlight'   => $this->input->post('news_hightlight', TRUE),
                    'news_pages'        => $this->input->post('news_pages', TRUE),
                    'pages_slug'        => $page_slug
                );

                $check = $this->MNews->create($strInsert);

                if(!$check)
                {
                    $data['msg'] = 'Lỗi khởi tạo';
                }
                else
                {
                    $this->native_session->set_flashdata('msg', 'Thêm tin tức thành công.');

                    redirect(base_url()."backend/news");

                    exit();
                }
            }//End else

            $this->_data['content'] = $this->load->view("backend/news/createnews", $data, TRUE);

            $this->load->view("backend/layout", $this->_data);

        }//End function createnews

        public function editnews()
        {
            $this->_data['title'] = 'Jewel | Sửa tin tức';

            if(!$this->uri->segment(4) || $this->uri->segment(4) == '')
            {
                show_404();

                exit();
            }

            $news_id = $this->uri->segment(4);

            $data['new'] = $this->MNews->select(array('news_id' => $news_id));

            if(!$data['new'])
            {
                $data['msg'] = 'Không tồn tại mã tin tức này.';
            }
            else
            {
                $this->load->model("backend/pages_model", "MPages", TRUE);

                $data['pages'] = $this->MPages->select();

                /*Validate data*/
                $this->form_validation->set_rules('news_title','Tiêu đề', 'trim|required|min_length[5]|xss_clean');
                $this->form_validation->set_rules('news_title_slug','Tiêu đề không dấu', 'trim|required|min_length[5]|xss_clean');
                $this->form_validation->set_rules('news_image_edit', 'Ảnh đại diện', 'trim|required|xss_clean');
                $this->form_validation->set_rules('news_image_thumb_edit', 'Thumbnail', 'trim|required|xss_clean');
                $this->form_validation->set_rules('news_content', 'Nội dung', 'trim|required|min_length[5]');
                $this->form_validation->set_message('required','<span style="font-weight: bold">%s</span> không được để trống');
                $this->form_validation->set_message('min_length','<span style="font-weight: bold">%s</span> không được ít hơn 5 ký tự');

                if($this->form_validation->run() == FALSE)
                {
                    $data['msg'] = '';
                }
                else
                {
                   foreach($data['pages'] as $page):
                        if($page->pages_id == $this->input->post('news_pages', TRUE)):
                            $page_slug = $page->slug;
                            break;
                        endif;
                    endforeach;

                    $strUpdate = array(
                        'news_title'        => $this->input->post('news_title', TRUE),
                        'news_title_slug'   => $this->input->post('news_title_slug', TRUE),
                        'news_image'        => $this->input->post('news_image_edit', TRUE),
                        'news_image_thumb'  => $this->input->post('news_image_thumb_edit', TRUE),
                        'news_content'      => $this->input->post('news_content'),
                        'news_status'       => $this->input->post('news_status', TRUE),
                        'news_hightlight'   => $this->input->post('news_hightlight', TRUE),
                        'news_pages'        => $this->input->post('news_pages', TRUE),
                        'pages_slug'        => $page_slug
                    );

                    $check = $this->MNews->edit($news_id, $strUpdate);

                    if(!$check)
                    {
                        $data['msg'] = 'Lỗi cập nhật.';
                    }
                    else
                    {
                        //NEED CHECK//
                        if($this->input->post('news_image_edit') != $data['new']->news_image):
                            if(is_file($data['new']->news_image)):
                                @unlink($data['new']->news_image);
                            endif;
                        endif;

                        if($this->input->post('news_image_thumb_edit') != $data['new']->news_image_thumb):
                            if(is_file($data['new']->news_image_thumb)):
                                @unlink($data['new']->news_image_thumb);
                            endif;
                        endif;

                        $this->native_session->set_flashdata('msg','Cập nhật tin tức thành công.');

                        redirect(base_url()."backend/news");

                        exit();
                    }
                }//End else
            }//End else

            $this->_data['content'] = $this->load->view("backend/news/editnews", $data, TRUE);

            $this->load->view("backend/layout", $this->_data);
        }//End function editnews

        public function delnews()
        {
            if(!$this->uri->segment(4) || $this->uri->segment(4) == '')
            {
                show_404();

                exit();
            }

            $news_id = $this->uri->segment(4);

            $check = $this->MNews->delete($news_id);

            if(!$check)
            {
                $this->native_session->set_flashdata('msg', 'Xoá không thành công');
            }
            else
            {
                $this->native_session->set_flashdata('msg', 'Xoá tin tức thành công');
            }

            redirect(base_url()."backend/news");
        }
        public function uploadify()
        {
            $this->load->helper('uploadify');

    		$img_data = img_uploadify();

    		$size = getimagesize($img_data['full_path']);

            if($size[0] < 100 || $size[1] < 50)
            {
                @unlink($img_data['full_path']);
                echo 1;
            }
            else
            {
                $img_data = array(
                    'file_name' => $img_data['file_name'],
                    'width'     => $size[0],
                    'height'    => $size[1],
                    'fullpath'  => $img_data['full_path']
                );

                echo json_encode($img_data);
            }
        }//End function uploadify

        public function processimg()
        {
            /*Load jCrop helper*/
            $this->load->helper('jcrop');

            $thumb_image = jcrop();

            $img_parts = pathinfo($thumb_image);

            $img_data = array(
                'img_name'  => $img_parts['basename']
            );

            echo  json_encode($img_data);
        }//End processimg

        public function rejectimg()
        {
             if(is_file($this->input->post('filepath')))
                @unlink($this->input->post('filepath'));
        }//End rejectimg



	}

/* End of file News.php */
/* Location: ./controllers/backend/news.php */